package com.indexingsystem.db.mongoDB.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;

import com.alibaba.fastjson.JSON;
import com.indexingsystem.boss.Vo.InstitutionsListVo;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoDBUtils;
import com.indexingsystem.db.mongoDB.comon.MongoTableName;
import com.indexingsystem.db.mongoDB.po.InstitutionsTreePo;
import com.indexingsystem.db.mongoDB.po.ResourceCategorsResPo;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * 机构mogo实现类.
 * 
 * @author Administrator
 * 
 */
public class InstitutionsMongo {

	/**
	 * 
	 * @Title: addInstitutions
	 * @see: TODO
	 * @param Institutions
	 * @return
	 * @return boolean 返回类型
	 * @throws
	 */
	public static void addInstitutions(List<InstitutionsListVo> institutionsListVoList) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_INSTITUTIONS_TREE + "添加 ------->" + com.alibaba.fastjson.JSON.toJSONString( institutionsListVoList, true ) );
		List<Document> addObjList = new ArrayList<Document>();
		if (null != institutionsListVoList && institutionsListVoList.size() > 0) {
			for (InstitutionsListVo institutionsListVo : institutionsListVoList) {
				Document addObj = new Document();
				// addObj.put( "_id", resCateResPo.getResCategoryId() );
				addObj.put( "institutionId", institutionsListVo.getInstitutionId() );
				addObj.put( "parentId", institutionsListVo.getParentId() );
				addObj.put( "institutionOrder", institutionsListVo.getInstitutionOrder() );
				addObj.put( "institutionName", institutionsListVo.getInstitutionName() );
				addObj.put( "enabled", institutionsListVo.getEnabled() );
				addObjList.add( addObj );
			}

		}
		manager.addList( addObjList, MongoTableName.DH_INSTITUTIONS_TREE ); // 向mongodb插入数据
	}

	/**
	 * 获取机构树形数据.
	 * 
	 * @param map
	 * @return
	 */
	public static List<InstitutionsListVo> findInstitutionsAll(Map<String, Object> map) {
		MongoManager manager = new MongoManager();
		List<InstitutionsListVo> institutionsListVoList = null;
		LogCvt.debug( MongoTableName.DH_INSTITUTIONS_TREE + "查询机构树形 ------->" );
		List<Document> documentsList = manager.findAll( InstitutionsListVo.class, MongoTableName.DH_INSTITUTIONS_TREE );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				institutionsListVoList = new ArrayList<InstitutionsListVo>();
				for (Document document : documentsList) {
					InstitutionsListVo institutionsListVo = new InstitutionsListVo();
					institutionsListVo = (InstitutionsListVo) MongoDBUtils.setFieldValue( institutionsListVo, document );
					if (!institutionsListVoList.contains( institutionsListVo )) {
						institutionsListVoList.add( institutionsListVo );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		LogCvt.debug( "查询 InstitutionsListVo 结果:" + JSON.toJSONString( institutionsListVoList, true ) );
		return institutionsListVoList;
	}

	/**
	 * 获取机构树形数据.
	 * 
	 * @param map
	 * @return
	 */
	public static void deleteInstitutionsAll() {
		MongoManager manager = new MongoManager();
		manager.deleteAll( MongoTableName.DH_INSTITUTIONS_TREE );
	}
}